package org.tang.springcloudvv.util;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.tang.springcloudvv.vo.User;

/**
 * <p><em>Created on 2022/10/14 11:01 上午</em></p>
 *
 * @author fuzhongtang
 * @since 1.0.1
 */
public class SessionUtil {

    public static User getUser()
    {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null)
        {
            Object userLogin = authentication.getPrincipal();
            if (userLogin != null)
            {
                User user = new User();
                try {
                    BeanUtils.copyProperties(userLogin,user);
                }
                catch (Exception e)
                {}
                if (userLogin instanceof UserDetails)
                {
                    UserDetails userDetails = (UserDetails)userLogin;
                    user.setUserName(userDetails.getUsername());
                }

                return user;
            }
        }
        return null;
    }
}
